/*
 * Copyright (c) 2020
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-08-04     chao         the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#if defined BSP_USING_LED

#define LED_PIN             GET_PIN(E, 5)

/* 
 * @brief
 * @param
 * @retval
 */
int led_init(void)
{
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_write(LED_PIN, PIN_HIGH);
    return RT_EOK;
}
INIT_APP_EXPORT(led_init);

/* 
 * @brief
 * @param
 * @retval
 */
void led_on(void)
{
    rt_pin_write(LED_PIN, PIN_LOW);
}

/* 
 * @brief
 * @param
 * @retval
 */
void led_off(void)
{
    rt_pin_write(LED_PIN, PIN_HIGH);
}

/* 
 * @brief
 * @param
 * @retval
 */
void led_toggle(void)
{
    int value = rt_pin_read(LED_PIN);

    if (value)
    {
        rt_pin_write(LED_PIN, PIN_LOW);
    }
    else
    {
        rt_pin_write(LED_PIN, PIN_HIGH);
    }
}

#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
/* 
 * @brief
 * @param
 * @retval
 */
static void led(uint8_t argc, char **argv)
{
    if (argc == 2)
    {
        if (rt_memcmp(argv[1], "on", 2)==0)
        {
            led_on();
        }
        else if (rt_memcmp(argv[1], "off", 3)==0)
        {
            led_off();
        }
        else if (rt_memcmp(argv[1], "toggle", 6)==0)
        {
            led_toggle();
        }
    }
    else
    {
        rt_kprintf("please input: led on/off/toggle\n");
        rt_kprintf("e.g: led on\n");
    }
}
MSH_CMD_EXPORT(led, led control on/off/toggle);
#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */

#endif /* BSP_USING_LED */



